package com.hm.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hm.reggie.common.R;
import com.hm.reggie.entity.Category;
import com.hm.reggie.entity.Employee;
import com.hm.reggie.entity.Setmeal;
import com.hm.reggie.service.CategoryService;
import com.hm.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import java.util.List;

/**
 *分类
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 分类分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> page(int page, int pageSize){
        //log.info("page:{},pageSize:{},name:{}",page,pageSize);
        //构造分页构造器
        Page<Category> pageInfo =new Page<>(page,pageSize);

        //条件封装,添加条件
        LambdaQueryWrapper<Category> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        //lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Category::getName,name);
        lambdaQueryWrapper.orderByAsc(Category::getSort);

        //执行查村
        categoryService.page(pageInfo,lambdaQueryWrapper);

        return R.success(pageInfo);
    }


    /***
     * 修改实体类
     * @param category
     * @return
     */
    @PutMapping
    public R put(@RequestBody Category category){
        log.info("====={put}=====",category.toString());
        boolean b = categoryService.saveOrUpdate(category);
        return R.success(categoryService.getById(category.getId()));
    }

    /**
     * 新增category
     */
    @PostMapping
    public R<Category> post(@RequestBody Category category){
        log.info("====={post}=====",category.toString());
        boolean b = categoryService.saveOrUpdate(category);
        return R.success(categoryService.getById(category.getId()));
    }

    /***
     * 删除实体类
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){

        categoryService.remove(ids);
        return R.success("删除成功");

    }

    /**
     * 返回菜品的list集合
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        //构造条件
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        categoryLambdaQueryWrapper.orderByAsc(Category::getSort);
        //执行搜索
        List<Category> list = categoryService.list(categoryLambdaQueryWrapper);


        return R.success(list);
    }


}
